﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Saxon.Api;

namespace WMM.Tapioca.Formula
{
    /// <summary>
    /// A parameter is declared by a <variable:parameter> element and can be given default values, 
    /// specified as part of their declaration, or values that are supplied by processing software.
    /// </summary>
    public class Parameter : FormulaResource
    {
        private string name;

        /// <returns> QName of the parameter being declared. </returns>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string typeParameter;

        /// <returns>A parameter declaration MAY contain an @as attribute that specifies
        /// the data type required by the parameter. </returns>
        public string TypeParameter
        {
            get { return typeParameter; }
            set { typeParameter = value; }
        }

        private string value;

        /// <returns> value of parameter. </returns>
        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }        

        private bool required;

        /// <returns>If the @required attribute on a parameter declaration is equal to true, then the parameter is mandatory; 
        /// its value MUST be supplied by the processing application. </returns>
        public bool Required
        {
            get { return required; }
            set { required = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public Parameter(Linkbase.Resource resource)
            : base(resource)
        {
            foreach (XAttribute attr in resource.ResourceXElement.Attributes())
            {
                if (attr.Name.LocalName.Equals("name"))
                {
                    name = attr.Value;
                }

                if (attr.Name.LocalName.Equals("as"))
                {
                    typeParameter = attr.Value;
                }

                if (attr.Name.LocalName.Equals("select"))
                {
                    value = EvaluateExpression(attr.Value);
                }

                //if (attr.Name.LocalName.Equals("xmlns"))
                //{
                //    xmlns = attr.Value;
                //}

                if (attr.Name.LocalName.Equals("required"))
                {
                    required = Boolean.Parse(attr.Value);
                }
            }
        }


        protected string EvaluateExpression(string expression)
        {
            // Create a Processor instance.
            Processor processor = new Processor();

            //RegisterExtensionFunctions(processor);

            // Create the XPath expression.
            XPathCompiler compiler = processor.NewXPathCompiler();
            //compiler.DeclareNamespace("xfi", "http://www.xbrl.org/2008/function/instance");

            XPathSelector selector = compiler.Compile(expression).Load();

            //SetContextItem(processor, selector, dts.Instance.ThisXDocument.Root.GetXmlNode());

            string result = selector.EvaluateSingle().ToString();

            return result;
        }

    }
}
